package com.hz.framework.util;

import   java.io.*;   
import   com.sun.image.codec.jpeg.*;   
import   java.awt.image.*;   
import   java.awt.*;   
import   java.applet.*;   


public class OperatePicture {
	
//	BufferedImage sourceImg = javax.imageio.ImageIO.read(is); 
//	得到BufferedImage对象，其中的is是InputStream，用来接收图片数据的流。   
//	取图像宽：sourceImg.getWidth()   
//	取图像高：sourceImg.getHeight() 
//	s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)；
	String   InputDir;   
    String   OutputDir;   
    String   InputFileName;   
    String   OutputFileName;   
    int   OutputWidth=80;   
    int   OutputHeight=80;   
    int   rate=0;   

    public   OperatePicture(){   
                    InputDir="";   
                    OutputDir="";   
                    InputFileName="";   
                    OutputFileName="";   
                    OutputWidth=80;   
                    OutputHeight=80;   
                    rate=0;   
    }   

    public   void   setInputDir(String   InputDir){   
                    this.InputDir=InputDir;   
    }   

    public   void   setOutputDir(String   OutputDir){   
                    this.OutputDir=OutputDir;   
    }   

    public   void   setInputFileName(String   InputFileName){   
                    this.InputFileName=InputFileName;   
    }   

    public   void   setOutputFileName(String   OutputFileName){   
                    this.OutputFileName=OutputFileName;   
    }   

    public   void   setOutputWidth(int   OutputWidth){   
                    this.OutputWidth=OutputWidth;   
    }   

    public   void   setOutputHeight(int   OutputHeight){   
                    this.OutputHeight=OutputHeight;   
    }   

    public   void   setW_H(int   width,int   height){   
                    this.OutputWidth=width;   
                    this.OutputHeight=height;   
    }   

    public   String   s_pic(){  
    	
                    File   file   =   new   File(OutputDir+OutputFileName);   
                    
                    FileOutputStream   tempout   =null;   
                    try{   
                        tempout=   new   FileOutputStream(file);   
                    }catch(Exception   ex){   
                        System.out.println(ex.toString());   
                    }   
                    Image   img=null;   
                    Toolkit   tk=Toolkit.getDefaultToolkit();   
                    Applet   app=new   Applet();   
                    MediaTracker   mt   =   new   MediaTracker(app);   
                    try   {   
                                    img=tk.getImage(InputDir+InputFileName);   
                                    mt.addImage(img,   0);   
                                    mt.waitForID(0);   
                    }catch(Exception   e)   {   
                                    e.printStackTrace();   
                    }   

                    if(img.getWidth(null)==-1){   
                                    System.out.println("       can't   read,retry!"+"<BR>");   
                    }else{   
                                    double   rate1=((double)img.getWidth(null))/OutputWidth;   
                                    double   rate2=((double)img.getHeight(null))/OutputHeight;   
                                    double   rate=rate1>rate2?rate1:rate2;  
//                                  int   new_w=(int)(((double)img.getWidth(null))/rate);   
//                                  int   new_h=(int)(((double)img.getHeight(null))/rate); 
                                    int   new_w=OutputWidth;   
                                    int   new_h=OutputHeight; 
                                    
                                    BufferedImage   buffImg   =   new   BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);   
                                    Graphics   g   =   buffImg.createGraphics();   
                                    g.setColor(Color.black);  
                                    g.fillRect(0,0,new_w,new_h);   

                                    g.drawImage(img,0,0,new_w,new_h,null);   
                                    g.dispose();   

                                    JPEGImageEncoder   encoder   =   JPEGCodec.createJPEGEncoder(tempout);   
                                    try{   
                                                    encoder.encode(buffImg);   
                                                    tempout.close();   
                                    }catch(IOException   ex){   
                                                    System.out.println(ex.toString());   
                                    }   
                    }   
                    return   "ok";   
    }   

    public   String   s_pic(String   InputDir,String   OutputDir,String   InputFileName,String   OutputFileName){   
                    this.InputDir=InputDir;   
                    this.OutputDir=OutputDir;   
                    this.InputFileName=InputFileName;   
                    this.OutputFileName=OutputFileName;   
                    return   s_pic();   
    }   

    public   String   s_pic(String   InputDir,String   OutputDir,String   InputFileName,String   OutputFileName,int   width,int   height){   
                    this.InputDir=InputDir;   
                    this.OutputDir=OutputDir;   
                    this.InputFileName=InputFileName;   
                    this.OutputFileName=OutputFileName;   
                    setW_H(width,height);   
                    return   s_pic();   
    }  
}
